package com.jservice.framework.authc;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jservice.model.SysUser;
import com.jservice.web.base.JBaseController;

/**
 * AuthcInterceptor - 权限拦截器
 *
 * @Author cwledit
 * @DATE 2017/5/27.
 */
public class AuthcInterceptor implements Interceptor{
    @Override
    public void intercept(Invocation inv) {
        String key = inv.getActionKey();
        JBaseController c = (JBaseController) inv.getController();
        //获得当前用户
        SysUser cUser = c.getCUser();
        if (cUser == null){
            c.redirectLogin();
            return;
        }else{
            inv.invoke();
            return;
        }
//        if (cUser.isAdmin()){
//            inv.invoke();
//            return;
//        }
//        List<String> urls = CacheKit.get("authc", "authc_urls_" + cUser.getId(), () -> {
//            List<String> perms = SysRoleService.me().findPermissionsByUserId(cUser.getId());
//            Set<String> urls1 = Sets.newHashSet();
//            for (String perm : perms){
//                AuthcConfig authcConfig = AuthcUtil.getAuthcConfig(perm);
//                if (authcConfig != null){
//                    Set<String> urlSet = authcConfig.getUrls();
//                    urls1.addAll(urlSet);
//                }
//            }
//            return urls1;
//        });
//        if (urls.contains(key)){
//            inv.invoke();
//            return;
//        }else{
//            LogKit.debug("权限验证不通过："+cUser.getLoginName()+"  key："+ key);
//            return;
//        }
    }
}
